<cfif Not isDefined("session.expid")>
	<cfoutput>
		Unknown Experiment ID
	</cfoutput>
	<cfabort>
</cfif>

<cfif Not isDefined("Qid")>
	<cfoutput>
		Unknown Questionnaire ID
	</cfoutput>
	<cfabort>
</cfif>

<cfif Not isDefined("session.pid")>
	<cfset session.message = "Incorrect starting place">
	<cflocation url="ethics.cfm?id=#session.expid#">
</cfif>

<cfquery name = "ww" datasource="exp">
	
	SELECT id, question, istext, likertid, radioid, questid
    FROM question
	WHERE questid = <cfqueryparam cfsqltype="cf_sql_integer" value="#Qid#">
	ORDER BY id

</cfquery>

<cfif IsDefined("form.subAns")>	
	
	<!---needed to choose next question --->
	<!--- Stores the participants answers for each question --->
	
	<cfloop query="ww">
	
		
		<cfset hid_pan = "hid_pans_" & id>
		
		
		<cfif IsDefined("hid_pan")>
		
			<cfset temp = form['#hid_pan#']>
			
			<cfif IsDefined("form.a_" & temp)>
			
				<cfset answer_rand = form['a_#temp#']>
				
				<cfquery datasource="exp">	
				
					INSERT INTO givesanswerq( participantid, questid, answer )
					VALUES( #session.pid#, #ww.id#, <cfqueryparam cfsqltype="cf_sql_varchar" value="#answer_rand#">)
					
				</cfquery> 
			</cfif>
		</cfif>
	</cfloop>

	<cfquery name="exptype" datasource="exp">
		SELECT *
		FROM questionnaire
		WHERE id = #ww.questid#
	</cfquery>
	
	<cfif #exptype.isend# eq 1>
		<cflocation url="/ecada/thanks.cfm?id=#session.expid#">
	<cfelseif #exptype.isstart# eq 1>
		<!--- Goes to next page --->
		<cfset session.prev = "">
		<cfset session.count += 1>
		<cfset session.hour = #Hour(Now())#>	
		<cfset session.minute = #Minute(Now())#>	
		<cfset session.second = #Second(Now())#>
		<cfset id = session.expid >
		<!--- redirects to next page --->
		<cflocation url="/ecada/exppageV2.cfm?id=#id#">
	<cfelse>
		<!---some unknown state--->
		<cfoutput>
			Unknown
		</cfoutput>
		
		<cfabort>
	</cfif>

    
<cfelse>

	<cfoutput> 

		<cfinclude template= "/ecada/pheader.cfm">

		<div style="width: 100%; padding-top: 16px; margin-left: auto; margin-right: auto;">
			<center>
			<form method = "post" id="form" name="form" action = "/ecada/quest.cfm?Qid=#Qid#"> 
				<table>
					
					<cfloop query = ww>

						<!---<cfset session.qcount += 1 >--->

						<!--- THIS IS WHAT THE PAGE WILL OUTPUT --->
						
						<tr>
							<td><center>#ww.question#</center></td>
						</tr>
						<tr>
								<cfset chars = "0123456789abcdefghiklmnopqrstuvwxyz" / >
								<cfset strLength = 6 / >
								<cfset randout = "" / >

								<cfloop from="1" to="#strLength#" index="i">
									<cfset rnum = ceiling(rand() * len(chars)) / >
									<cfif rnum EQ 0 ><cfset rnum = 1 / ></cfif>
									<cfset randout = randout & mid(chars, rnum, 1) / >
								</cfloop>
								
								<input type="hidden" name="hid_pans_#id#" id="hid_pans_#id#" value="#randout#">
								<cfif #radioid# eq 1>
								<td>
									<center>
									<cfquery name="radio" datasource="exp">
									
										SELECT r.text
										FROM radio r, radiogroup rg
										WHERE rg.radioID = r.id
										AND rg.questID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#id#">
									
									</cfquery>
									<div id="#randout#_div">
										<cfset index = 0>
										<cfloop query="radio">
											<input type="radio" id="#randout##index#" name="a_#randout#" value="#text#" />
											<label for="#randout##index#">#text#</label>
											<cfset index += 1>
										</cfloop>
									</div>
									<script>
										$(function() {
											$( "###randout#_div" ).buttonset();
										});
									</script>
									</center>
								</td>
								<cfelseif #likertid# neq 0>
									<cfquery datasource="exp" name="likert">
									
										SELECT *
										FROM likert lkrt
										WHERE id = #likertid#
										
									</cfquery>
									<cfquery datasource="exp" name="likertsteps">
									
										SELECT *
										FROM likertsteps 
										WHERE likertid = #likertid#
										
									</cfquery>
									<style type="text/css">
									
										##likert { text-align:center; }
										##likert td { width: 70px; }
									
									</style>
									<td>
										<center>
										<table id="likert">
											<tr>
												<td>#likert.starttext#</td>
												<cfset index = 0>
												<cfloop query="likertsteps">
													<td>
														<input type="radio" name="a_#randout#" value="#index#" />
														<cfset index += 1 >
													</td>
												</cfloop>
												<td>#likert.endtext#</td>
											</tr>
											<tr>
												<td></td>
												<cfloop query="likertsteps">
													<td>#steptext#</td>
												</cfloop>
												<td></td>
											</tr>
										</table>
										</center>
									</td>
								<cfelseif #istext# eq 1>
									<td>
										<center>
											<input style="width: 325px;" type="text" name="a_#randout#" id="a_#randout#"  value="">
										</center>
									</td>
								</cfif>								
						</cfloop>
						
					</tr>
					<tr>

						<td>
							<button class="ui-state-default ui-corner-all" style="float:right; width: 100px; height: 50px;" type="submit" value="" name="subAns">Next</button>
						</td>
					</tr>
				</table>
			</form>   
			</center>
		</div>
	</cfoutput>
</cfif>   

<!--- END OF PAGE OUTPUT --->
